<template>
	<view class="container bar">
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{data.title}}</block>
			<block slot="right">
				<view class="action" @click="viewsearch=!viewsearch">
					<text class="cuIcon-search text-lg text-boldm" :class="viewsearch?'text-yellow':''"></text>
				</view>
			</block>
		</cu-custom>
		<view class="border-top padding margin-top-xs">
			<view class="cu-bar search" v-if="viewsearch">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input type="text" v-model="keyword" placeholder="输入讲师关键字搜索" @confirm="viewlector(1)" confirm-type="search"></input>
				</view>
				<view class="action">
					<button class="cu-btn bg-gradual-yellow shadow-blur round" @click="viewlector(1)">搜索</button>
				</view>
			</view>
			
			<view class="text-center padding-xl margin-top" style="line-height: 400upx;" v-if="data.lecturer.length==0">
				<text class="text-gray text-lg padding-top-xl" v-if="keyword">少年，换个关键字吧~</text>
				<text class="text-gray text-lg padding-top-xl" v-else>赶快成为{{systemname}}第一个讲师吧~</text>
			</view>
			<view class="cu-list menu-avatar comment user-card" v-else>
				
				<view class="cu-item" v-for="(item, index) in data.lecturer" :key="index">
					<view @click="naviTo('lector/detail',{id:item.id})" class="cu-avatar round xl" :style="'background-image:url('+item.lecturer_Img+');'"></view>
					<view class="content">
						<view class="text-bold">
							<!-- 标题 -->
							{{item.name}}
							<button class="cu-btn round sm right" @click="doFavor(item.id,index)" :class="item.hasfavor?'line-gray':'line-yellow'"><text class="cuIcon-add"></text>{{item.hasfavor?'已关注':'关注'}}</button>
						</view>
						<!-- 个人介绍 -->
						<view @click="naviTo('lector/detail',{id:item.id})" class="text-muted text-content text-df">
							{{item.intro}}
						</view>
					</view>
				</view>
			</view>
			<view class="padding text-center" v-if="viewmore">
				<view class="text-yellow text-lg" @click="viewlector(viewpage+1)">更多优秀讲师</view>
			</view>
		</view>
		<view class="cu-bar tabbar">
			<view class="action">
				<button @click="naviTo('lector/'+(data.apply.audit==1?'ole':'apply'))" v-if="lecturer.audit!=2" class="cu-btn lg round margin-bottom-xl" :class="data.apply.status==1?'bg-gradual-blue':'bg-gradual-red'">{{data.apply.status==1?'进入讲师中心':'申请成为讲师'}}</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import core from "@/static/core.js"
	
	export default {
		computed: mapState(['hasLogin']),
		data() {
			return {
				lecturer:{},
				viewpage:1,
				viewsearch:false,
				dian:true,
				viewmore:true,
				keyword:'',
				systemname:'平台',
				data:{
					title:"讲师风采",
					lectors:[],
					lecturer:[],
					img:"",
					apply:{
						id:0,
						status:0
					},
					msg:""
				},
				u:{
					name:""
				}
			}
		},
		onLoad() {
			// this.jiangshi();
			this.systemname = core.system.name;
			var that = this;
			this.initData()
			this.initlecture()
		},
		onShow() {
			
				this.selLike()
		},
	
		onPullDownRefresh(){
			var that = this;
			this.initData('lector',{},function(res){
				that.viewpage = 1;
			});
			uni.stopPullDownRefresh();
		},
		methods:{
			async initData(){
				let that = this;
				// core.get(route,function(res){
				// 	console.log(res)
				// 	if(typeof(res.message)!='undefined' && typeof(res.type)!='undefined'){
				// 		return core.report(res, true);
				// 	}
				// 	that.data = res;
				// 	that.data.msg="asd"
				// 	that.viewmore = res.lectors.length<15 ? false : true;
				// 	uni.setNavigationBarTitle({
				// 		title:res.title,
				// 	});
				// 	if(callback){
				// 		callback(res);
				// 	}
				// },data);
				that.data.lecturer=(await this.$api.reqGet({
					url:"lecturer/selall",
				})).data;
				this.selLike();
			},
			/*查询关注讲师表*/
			async selLike(){
				var that = this;
				if(core.userinfo.uid!=0){
					if((await this.$api.reqGet({url:"lecturer/selLike/"+core.userinfo.uid,})).data===null||
						(await this.$api.reqGet({url:"lecturer/selLike/"+core.userinfo.uid,})).data===0){
						console.log("还没有此讲师的关注")
					}else{
						var sel = (await this.$api.reqGet({
							url:"lecturer/selLike/"+core.userinfo.uid,
						})).data;
						for(var i = 0;i<sel.length;i++){
							for(var j = 0; j<that.data.lecturer.length;j++){
								if(that.data.lecturer[j].id==sel[i].lecturerId){
									that.data.lecturer[j].hasfavor = true;
									console.log(that.data.lecturer[j].hasfavor)
								}
							}
						}
					}
				}
			},
			naviTo(page,data={}){
				return core.navito(page,data);
			},
			
			/*关注讲师 关注讲师*/
			async doFavor(id,index){
				if(core.userinfo.uid<=0) return core.toast('请先登录','login');
				var that = this;
				that.data.lecturer[index].hasfavor = !(that.data.lecturer[index].hasfavor)
				// return core.get('lector/favor',function(res){
				// 	if(res.type!='success') return core.report(res);
				// 	that.data.lectors[index].hasfavor = res.message.id>0 ? true : false;
				// },{uid:uid})
				/*关注讲师 关注讲师*/
				var a = (await this.$api.reqPost({
					url:"/lecturer/ins",
					data:{
					uid:core.userinfo.uid,
					lecturerId:id,
					}
				})).data;
			},
			viewlector(page=1){
				var that = this;
				core.get('lector',function(res){
					that.viewpage = page;
					if(page==1){
						that.data = res;
					}else{
						that.data.lectors = that.data.lectors.concat(res.lectors);
					}
					that.viewmore = res.lectors.length<15 ? false : true;
				},{page:page,keyword:this.keyword});
			},
			async initlecture(){
				var that = this;
				console.log(core.userinfo.uid)
				var a=(await this.$api.reqGet({
					url:"lecturerapply/sel/"+core.userinfo.uid,
					methods:"GET"
				})).data;
				if(a!=null){
				that.lecturer=a;
				console.log(that.lecturer);
				}
				},
				
			// async gunazhu(lecturer){
			// 		var that = this
			// 		alert(111)
					
			// 		console.log(lecturer)
			// 		console.log(that.data.lecturer[lecturer].message)
			// 		console.log(lecturer)
			// 		that.data.lecturer[lecturer].hasfavor = that.data.lecturer[lecturer].message>0?false:true;
			// 		// console.log(lecturer)
			// 		// if(that.data.lecturer[lecturer].message==0){
			// 		// 	var page = lecturer+1
			// 		// 	var a=(await this.$api.reqGet({
			// 		// 		url:"/lecturer/upHasfavorFalse/"+page,
			// 		// 	})).data;
			// 		// alert("取消关注")
			// 		// 	var b=(await this.$api.reqGet({
			// 		// 		url:"/lecturer/upmessage1/"+page,
			// 		// 	})).data;
			// 		// }
			// 		// if(that.data.lecturer[lecturer].message==1){
			// 		// 	var page = lecturer+1
			// 		// 		let that =this
			// 		// 		var a=(await this.$api.reqGet({
			// 		// 			url:"/lecturer/upHasfavorTrue/"+page,
			// 		// 		})).data;
			// 		// 		alert("取消关注")
			// 		// 		var b=(await this.$api.reqGet({
			// 		// 			url:"/lecturer/upmessage0/"+page,
			// 		// 		})).data;
			// 		// }
			// },
			async tabSelect(e){
				that.data.lessons = (await this.$api.reqGet({url:"/menu/courseInfo/"+tid})).data;
			}
		}
	}
</script>

<style>
	page{background: #FFFFFF;}
	.container.bar{min-height:calc(100vh - 100upx);}
</style>
